<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\model;

use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\BaseModel;

/**
 * 项目管理模型
 */
class Project extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_project';

    // public function getOrderAmountAttr($value)
    // {
    //     return number_format($value, 2, '.', ',');
    // }


    /**
     * 项目名称 搜索
     */
    public function searchProjectNameAttr($query, $value)
    {
        $query->where('project_name', 'like', '%' . $value . '%');
    }

    /**
     * 赞助企业名称 搜索
     */
    public function searchCompanyNameAttr($query, $value)
    {
        $query->where('company_name', 'like', '%' . $value . '%');
    }

    public function getCollaboratorByAttr($value)
    {
        return SystemUser::whereIn('id', $value)->field('id,username,nickname')->select();
    }

    // 关联用户 多对多
    public function users()
    {
        return $this->belongsToMany(Users::class, ProjectUsers::class, 'user_id', 'project_id');
    }

    public function usersOne()
    {
        return $this->hasOne(Users::class, 'id', 'user_id');
    }

    /**
     * 关联后台用户
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-11
     * @return void
     */
    public function adminUser()
    {
        return $this->hasOne(SystemUser::class, 'id', 'created_by');
    }

    /**
     * 项目下的所有会议 一对多
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-05
     * @return void
     */
    public function projectMeeting()
    {
        return $this->hasMany(ProjectMeeting::class, 'project_id', 'id');
    }

    // 定义与 User 模型的多对多关联
    public function usersProject()
    {
        return $this->belongsToMany(Users::class, ProjectUsers::class, 'user_id', 'project_id');
    }
}
